package com.order.manager.controller.Evaluate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.order.manager.Model.ShopExamineInfo;
import com.order.manager.Model.other.RestBean;
import com.order.manager.service.impl.ShopExamineInfoServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 店铺审核信息控制器
 * 处理店铺审核信息的提交和查询操作
 */
@RequestMapping("/api/examineShop")
@RestController
public class ShopExamineInfoController {

    @Resource
    ShopExamineInfoServiceImpl shopExamineInfoService;
    @RequestMapping("/apply")
    public RestBean shopExamineInfoApply(@RequestBody ShopExamineInfo shopExamineInfo) {
        // 设置信息提交时间为当前时间
        shopExamineInfo.setInputTime(new Date());
        // 检查数据库中是否已存在该店铺的审核记录
        if (shopExamineInfoService.count(new QueryWrapper<ShopExamineInfo>().eq("shop_id", shopExamineInfo.getShopId())) != 0) {
            // 存在则更新记录
            return shopExamineInfoService.updateById(shopExamineInfo)
                    ? RestBean.success(shopExamineInfo)
                    : RestBean.failure(500, "店铺审核信息更新失败");
        }
        // 不存在则创建新记录
        return shopExamineInfoService.save(shopExamineInfo)
                ? RestBean.success(shopExamineInfo)
                : RestBean.failure(500, "店铺审核信息保存失败");
    }

    /**
     * 根据店铺ID查询审核信息
     *
     * @param shopId 店铺ID，通过路径变量传入
     * @return 操作结果：成功返回审核信息，失败返回错误码和消息
     */
    @RequestMapping("/get/{shopId}")
    public RestBean shopExamineInfoGet(@PathVariable String shopId) {
        // 通过店铺ID查询审核信息
        ShopExamineInfo shopExamineInfoServiceById = shopExamineInfoService.getById(shopId);

        // 判断是否存在记录并返回相应结果
        return shopExamineInfoServiceById != null
                ? RestBean.success(shopExamineInfoServiceById)
                : RestBean.failure(403, "不存在该店铺审核信息");
    }
}